function Player() {
    Unit.call(this);
	
	this.pos = { x: 0, y: 0 };
    this.size = { x: 16, y: 16 };
	this.realSize = { x: 16, y: 16 };
	this.spriteSize = { x: 32, y: 32 };

	this.speed =  175*2;
	this.color = "#0CED13";
    this.onStairs = true;
	this.attack = false;
	this.defense = false;
	this.die = false;	
	this.immunize = false;
	this.timerImmunize;
	
	this.valueAttack;
		
	this.update = function(elapsed, keysDown) {
	
		if(!this.isAlive()) {
			return;
		}
		
		var direction = -1;
        var move = { x: 0, y: 0 };
		
        if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && !(Keys.Down in keysDown)) {
            move.x -= this.speed * elapsed;
			direction = MOVE.LEFT;
        }
        else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Up in keysDown) && !(Keys.Down in keysDown)) { 
            move.x += this.speed * elapsed;
			direction = MOVE.RIGHT;
        }
        else if ((Keys.Up in keysDown) && !(Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Down in keysDown)) {
            move.y -= this.speed * elapsed;
			direction = MOVE.UP;
        }
        else if ((Keys.Down in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && !(Keys.Left in keysDown)) {
            move.y += this.speed * elapsed;
			direction = MOVE.DOWN;
        }
        else if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && (Keys.Up in keysDown) && !(Keys.Down in keysDown)) {
            move.x -= this.speed * elapsed;
            move.y -= this.speed * elapsed;
			direction = MOVE.LEFT;
        }
        else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && (Keys.Up in keysDown) && !(Keys.Down in keysDown)) { 
            move.x += this.speed * elapsed;
            move.y -= this.speed * elapsed;
			direction = MOVE.RIGHT;
        }
        else if ((Keys.Left in keysDown) && !(Keys.Right in keysDown) && !(Keys.Up in keysDown) && (Keys.Down in keysDown)) {
            move.x -= this.speed * elapsed;
            move.y += this.speed * elapsed;
			direction = MOVE.LEFT;
        }
        else if ((Keys.Right in keysDown) && !(Keys.Left in keysDown) && !(Keys.Up in keysDown) && (Keys.Down in keysDown)) { 
            move.x += this.speed * elapsed;
            move.y += this.speed * elapsed;
			direction = MOVE.RIGHT;
        }	

		if(direction >=0 && (!charSprite[ACTION.ATTACK].blockAction || !charSprite[ACTION.DEFENSE].blockAction)) {
			charSprite[ACTION.WALK].move(direction);
			charSprite[ACTION.ATTACK].move(direction);
			charSprite[ACTION.DEFENSE].move(direction);
			charSprite[ACTION.NONE].move(direction);
		}
		
		if (Keys.Attack in keysDown) { 			
			if(this.attack == true) 
				charSprite[ACTION.ATTACK].blockAction = true;
			else 					
				charSprite[ACTION.ATTACK].blockAction = false;
				
			this.attack = true;
			this.defense = false;
			this.die = false;
			charSprite[ACTION.DEFENSE].blockAction = false;
			
			this.actionAttack();
        }
		else if (Keys.Defense in keysDown) { 	
			if(this.defense == true) 
				charSprite[ACTION.DEFENSE].blockAction = true;
			else 						
				charSprite[ACTION.DEFENSE].blockAction = false;
				
			this.attack = false;
			this.defense = true;
			this.die = false;
			charSprite[ACTION.ATTACK].blockAction = false;
        }
		else {
			if(this.attack || this.defense) {
				charSprite[ACTION.WALK].stateAnim = 15;
				charSprite[ACTION.ATTACK].stateAnim = 15;
				charSprite[ACTION.DEFENSE].stateAnim = 15;
				charSprite[ACTION.NONE].stateAnim = 15;
			}
			this.attack = false;
			this.defense = false;
		}
		
		this.pos = levels[currentLevel].moveEntity(player.pos, player.size, player.realSize, move);
		this.posInMap.x = Math.floor((this.pos.x + (this.size.x / 2)) / tileSize);
		this.posInMap.y = Math.floor((this.pos.y + (this.size.y / 2)) / tileSize);
    };
	
	this.draw = function(canvas, context, camera) {
		if(!this.isAlive()) {
			return;
		}
		
		// draw the player
		if(this.attack == true) {
			charSprite[ACTION.ATTACK].drawSprite(context,
				Math.floor(this.pos.x - camera.x)-(tileSize-this.realSize.x)/2,
				Math.floor(this.pos.y - camera.y)-(tileSize-this.size.y)/2
				);
		}
		else if(this.defense == true) {
			charSprite[ACTION.DEFENSE].drawSprite(context,
				Math.floor(this.pos.x - camera.x)-(tileSize-this.realSize.x)/2,
				Math.floor(this.pos.y - camera.y)-(tileSize-this.size.y)/2
				);
		}
		else {
			charSprite[ACTION.WALK].drawSprite(context,
				Math.floor(this.pos.x - camera.x)-(tileSize-this.realSize.x)/2,
				Math.floor(this.pos.y - camera.y)-(tileSize-this.size.y)/2
				);
		}
	};
	
	this.actionAttack = function() {
		for (var i = levels[currentLevel].entity.length-1; i >= 0; i--) {
    
			if(levels[currentLevel].entity[i].isAlive()) {
				var ox = levels[currentLevel].entity[i].getPostionInMapX();
				var oy = levels[currentLevel].entity[i].getPostionInMapY();
				var px = this.getPostionInMapX();
				var py = this.getPostionInMapY();
				switch (charSprite[ACTION.WALK].direction) {
					case MOVE.LEFT :
						if(ox == px-1 && oy == py)
							levels[currentLevel].entity[i].setAttack(1);
						break;
					case MOVE.RIGHT :
						if(ox == px+1 && oy == py)
							levels[currentLevel].entity[i].setAttack(1);
						break;
					case MOVE.UP :
						if(ox == px && oy == py-1)
							levels[currentLevel].entity[i].setAttack(1);
						break;
					case MOVE.DOWN :
						if(ox == px && oy == py+1)
							levels[currentLevel].entity[i].setAttack(1);
						break;
				}
			}
		}
	};
	
	this.setValueAttack = function(value) {
		this.valueAttack = value;
	}
	
	this.setAttack = function(attack) {
		if(this.defense == false) {
			this.life -= attack;
			//this.setImmunize();
			if(this.life > 0)
				this.die = false;
			else
				this.die = true;
			
			return true;
		}
		else {
			return false;			
		}
	};
	
	this.setImmunize = function() {
		this.immunize = true;
		//this.timerImmunize = 
		setTimeout(this.immunize = false, 100); 
	}
}